#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

const std::string black_space_sep = " ";
const std::string protocol_sep = "\n";

std::string Encode(std::string& content)
{
  std::string package = std::to_string(content.size());
  package += protocol_sep;
  package += content;
  package += protocol_sep;

  return package;
}

bool Decode(std::string& package, std::string* content)
{
  auto pos = package.find(protocol_sep);
  if (pos == std::string::npos)
    return false;
  std::string len_str = package.substr(0, pos);
  int len = std::stoi(len_str);

  int total_len = len_str.size() + len + 2;
  if (package.size() < total_len) // 说明读取报文不完整
    return false;

  *content = package.substr(pos + 1, len);
  // 移除报文
  package.erase(0, total_len);

  return true;
}

class Request
{
public:
  Request(int x, int y, char opti) : _x(x), _y(y), _op(opti)
  {
  }
  Request()
  {
  }
  bool Serialization(std::string* out)
  {
#ifdef Myself
    // 序列化： 就是将struct => string
    std::string s = std::to_string(_x);
    s += black_space_sep;
    s += _op;
    s += black_space_sep;
    s += std::to_string(_y);

    *out = s;
    return true;

#else
    Json::Value v;
    v["x"] = _x;
    v["y"] = _y;
    v["op"] = _op;

    Json::FastWriter w;
    *out = w.write(v);
    return true;
#endif
  }
  bool Deserialization(std::string& in) // 到这里 报文已经是 "x op y"
  {
#ifdef Myself
    // 反序列化 : string => struct

    auto left = in.find(black_space_sep);
    if (left == std::string::npos)
      return false;
    std::string part_x = in.substr(0, left);
    _x = std::stoi(part_x);

    auto right = in.rfind(black_space_sep);
    if (right == std::string::npos)
      return false;
    std::string part_y = in.substr(right + 1);
    _y = stoi(part_y);

    if (left + 2 != right)
      return false;
    _op = in[left + 1];

    return true;
#else

    Json::Reader r;
    Json::Value v;
    r.parse(in, v);

    _x = v["x"].asInt();
    _y = v["y"].asInt();
    _op = v["op"].asInt();

    return true;

#endif
  }

  void DebugPrint()
  {
    std::cout << "新建任务成功: " << _x << _op << _y << "=?" << std::endl;
  }

public:
  int _x;
  int _y;
  char _op;
};

class Respond
{
public:
  Respond() {}
  Respond(int result, int code) :_result(result), _code(code)
  {
  }
  bool Serialization(std::string* out)
  {
#ifdef Myself
    // struct => string
    std::string s = std::to_string(_result);
    s += black_space_sep;
    s += std::to_string(_code);

    *out = s;
    return true;
#else
    Json::Value v;
    v["result"] = _result;
    v["code"] = _code;

    Json::FastWriter w;
    *out = w.write(v);
    return true;
#endif

  }
  bool Deserialization(std::string& in)
  {
#ifdef Myself
    auto left = in.find(black_space_sep);
    if (left == std::string::npos) return false;
    std::string result = in.substr(0, left);


    auto right = in.rfind(black_space_sep);
    if (right == std::string::npos) return false;
    std::string code = in.substr(right + 1);

    if (left != right) return false;

    _result = std::stoi(result);
    _code = std::stoi(code);

    return true;
#else
    Json::Value v;
    Json::Reader r;
    r.parse(in, v);

    _result = v["result"].asInt();
    _code = v["code"].asInt();

    return true;

#endif
  }

  void DubugPrint()
  {
    std::cout << "处理了一个计算任务, result: " << _result << ", code: " << _code << std::endl;
  }

public:
  int _result;
  int _code;
};